package com.client.view;

import com.client.bean.Message;
import com.client.bean.UserInfo;
import com.client.common.CmdControl;
import com.client.common.SendMsg;
import com.client.common.SendServer;
import com.client.db.UserDbOper;
import sun.applet.resources.MsgAppletViewer_ja;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

/**
 * Created by Administrator on 2016/12/19.
 */
public class FindView extends JFrame implements ActionListener{
	private JTextField txtCode , txtNickName , txtAge;
	private JComboBox cbSex;
	private JTable dataTable;
	private JButton btnFind , btnClose , btnAdd;
	private Vector vhead , vdata;
	private UserInfo selfuser;
	myTable mytable = null;

	public FindView(UserInfo selfuser) {
		super("查找好友");
		this.selfuser = selfuser;
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
		JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JLabel lblCode = new JLabel("帐号:");
		JLabel lblNickName = new JLabel("昵称:");
		JLabel lblAge = new JLabel("年龄");
		JLabel lblSex = new JLabel("性别:");

		txtCode = new JTextField(10);
		txtNickName = new JTextField(10);
		txtAge = new JTextField(5);

		cbSex = new JComboBox(new String[]{"男","女"});

		btnFind = new JButton("查找(F)");
		btnFind.setMnemonic('F');
		topPanel.add(lblCode);
		topPanel.add(txtCode);
		topPanel.add(lblNickName);
		topPanel.add(txtNickName);
		topPanel.add(lblAge);
		topPanel.add(txtAge);
		topPanel.add(lblSex);
		topPanel.add(cbSex);
		topPanel.add(btnFind);
		btnFind.addActionListener(this);
		add(topPanel , BorderLayout.NORTH);

		vhead = new Vector();
		vhead.add("帐号:");
		vhead.add("昵称:");
//		vhead.add("IP地址:");
//		vhead.add("端口:");
		vhead.add("年龄:");
		vhead.add("性别:");
		vhead.add("民族:");
		vhead.add("星座:");
		vhead.add("头像:");
		vhead.add("备注:");
//		vhead.add("信息:");
		vhead.add("状态:");

		vdata = new Vector();
		mytable = new myTable(vdata , vhead);
		dataTable = new JTable(mytable);
		add(new JScrollPane(dataTable));

		btnClose = new JButton("关闭(X)");
		btnClose.setMnemonic('X');
		btnAdd = new JButton("添加(A)");
		btnAdd.setMnemonic('A');

		JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		bottomPanel.add(btnAdd);
		bottomPanel.add(btnClose);
		add(bottomPanel , BorderLayout.SOUTH);
		btnAdd.addActionListener(this);
		btnClose.addActionListener(this);

		setSize(700 , 400);
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	}

//	public static void main(String[] args) {
//		new FindView();
//	}

	public void find(UserInfo userInfo) {
		vdata = new UserDbOper().find(userInfo);
		mytable.datas = vdata;
		dataTable.updateUI();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnFind) {
			UserInfo userInfo = new UserInfo();
			if (!txtCode.getText().equals("")) {
				userInfo.setCode(Integer.parseInt(txtCode.getText().trim()));
			}

			if (!txtNickName.getText().equals("")) {
				userInfo.setNicknName(txtNickName.getText().trim());
			}

			if (!txtAge.getText().equals("")) {
				userInfo.setAge(Integer.parseInt(txtAge.getText().trim()));
			}
			userInfo.setSex(cbSex.getSelectedItem().toString());
			find(userInfo);
		} else if (e.getSource() == btnAdd) {
			int row ,col;
			row = dataTable.getSelectedRow();
//			col = dataTable.getSelectedColumn();
			if (row < 0) {
				return;
			}
			Vector rowData = (Vector) vdata.get(row);

			int code = (Integer) rowData.get(0);
			UserInfo userInfo = new UserDbOper().findByCode(code);
			SendMsg msg = new SendMsg();
			msg.Cmd = CmdControl.CMD_ADDFRIEND;
			msg.friendUserInfo = userInfo;
			msg.selfUserInfo = selfuser;

			new SendServer().send(msg);

		} else if (e.getSource() == btnClose) {
			dispose();
		}
	}

	class myTable extends AbstractTableModel {
		Vector datas = new Vector();
		Vector columns = new Vector();
		public myTable(Vector datas , Vector colunms) {
			this.datas = datas;
			this.columns = colunms;
		}
		@Override
		public int getRowCount() {
			return datas.size();
		}

		@Override
		public int getColumnCount() {
			return columns.size();
		}

		public String getColumnName (int columnIndex) {
			return columns.get(columnIndex).toString();
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			Vector v = (Vector) datas.get(rowIndex);
			if (columnIndex == 6)//头像列返回图片
				return new ImageIcon(v.get(columnIndex).toString());
			else {
				return v.get(columnIndex);
			}
		}
	}
}
